import base64

from Crypto.Cipher import PKCS1_v1_5  # pip install pycryptodome
from Crypto.PublicKey import RSA


def encrypt_rsa(data, pub_key):
    data_bytes = data.encode("utf8")
    with open(pub_key, mode='r') as f:
        public_key_str = f.read()
        public_key = RSA.importKey(public_key_str)
        pk = PKCS1_v1_5.new(public_key)
        rsa_data = pk.encrypt(data_bytes)
        base64_data = base64.b64encode(rsa_data)
        return base64_data.decode("utf8")
